﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Jade.Tools.SL.Controls.Transitionnals
{
    /// <summary>
    /// Fade Effect UI Element
    /// </summary>
    public partial class FadeTransition : UserControl
    {
        private ContentPresenter newContentPresenter;
        private bool isTransitioning;

        #region Ctor
        public FadeTransition()
        {
            InitializeComponent();
            newContentPresenter = new ContentPresenter();
        }
        #endregion

        #region Public Methods
        public object ContentTransition
        {
            get { return (object)GetValue(ContentTransitionProperty); }
            set { SetValue(ContentTransitionProperty, value); }
        }
        public static readonly DependencyProperty ContentTransitionProperty =
            DependencyProperty.Register("ContentTransition",
                typeof(object),
                typeof(FadeTransition),
                new PropertyMetadata(null, OnContentTransitionChanged));
        #endregion

        #region Private Methods
        private static void OnContentTransitionChanged(object element, DependencyPropertyChangedEventArgs e)
        {
            FadeTransition te = element as FadeTransition;
            if (te != null)
            {
                te.BeginTransition();
            }
        }

        private void BeginTransition()
        {
            if (ContentTransition != null)
            {
                if (!isTransitioning)
                {
                    isTransitioning = true;
                    QuinticEase ee = new QuinticEase();
                    ee.EasingMode = EasingMode.EaseOut;

                    if (CtCenter.Content != null)
                    {
                        //oldContentPresenter
                        ScaleTransform scaleOld = new ScaleTransform();
                        CtCenter.RenderTransform = scaleOld;
                        CtCenter.RenderTransformOrigin = new Point(0.5, 0.5);

                        //newContentPresenter
                        ScaleTransform scale = new ScaleTransform();
                        newContentPresenter.RenderTransform = scale;
                        newContentPresenter.RenderTransformOrigin = new Point(0.5, 0.5);
                        newContentPresenter.Content = ContentTransition;
                        ((UIElement)ContentTransition).Opacity = 0.5;
                        scale.ScaleX = 0.9;
                        scale.ScaleY = 0.9;
                        GrdMain.Children.Add(newContentPresenter);

                        //Animation OldContent                      
                        DoubleAnimation oldScaleXContentAnimation = new DoubleAnimation();
                        oldScaleXContentAnimation.To = 0.9;
                        oldScaleXContentAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
                        oldScaleXContentAnimation.EasingFunction = ee;
                        Storyboard stOldScaleXContentAnimation = new Storyboard();
                        Storyboard.SetTargetProperty(oldScaleXContentAnimation, new PropertyPath(ScaleTransform.ScaleXProperty));
                        Storyboard.SetTarget(oldScaleXContentAnimation, scaleOld);
                        stOldScaleXContentAnimation.Children.Add(oldScaleXContentAnimation);
                        stOldScaleXContentAnimation.Begin();

                        DoubleAnimation oldScaleYContentAnimation = new DoubleAnimation();
                        oldScaleYContentAnimation.To = 0.9;
                        oldScaleYContentAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
                        oldScaleYContentAnimation.EasingFunction = ee;
                        Storyboard stOldScaleYContentAnimation = new Storyboard();
                        Storyboard.SetTargetProperty(oldScaleYContentAnimation, new PropertyPath(ScaleTransform.ScaleYProperty));
                        Storyboard.SetTarget(oldScaleYContentAnimation, scaleOld);
                        stOldScaleYContentAnimation.Children.Add(oldScaleYContentAnimation);
                        stOldScaleYContentAnimation.Begin();

                        DoubleAnimation oldOpacityContentAnimation = new DoubleAnimation();
                        oldOpacityContentAnimation.To = 0.5;
                        oldOpacityContentAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(700));
                        oldOpacityContentAnimation.EasingFunction = ee;
                        Storyboard stOldOpacityContentAnimation = new Storyboard();
                        Storyboard.SetTargetProperty(oldOpacityContentAnimation, new PropertyPath(ContentPresenter.OpacityProperty));
                        Storyboard.SetTarget(oldOpacityContentAnimation, CtCenter);
                        stOldOpacityContentAnimation.Children.Add(oldOpacityContentAnimation);
                        stOldOpacityContentAnimation.Begin();

                        //Animation NewContent
                        DoubleAnimation newScaleXContentAnimation = new DoubleAnimation();
                        newScaleXContentAnimation.To = 1;
                        newScaleXContentAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(700));
                        newScaleXContentAnimation.EasingFunction = ee;
                        Storyboard stNewScaleXContentAnimation = new Storyboard();
                        Storyboard.SetTargetProperty(newScaleXContentAnimation, new PropertyPath(ScaleTransform.ScaleXProperty));
                        Storyboard.SetTarget(newScaleXContentAnimation, scale);
                        stNewScaleXContentAnimation.Children.Add(newScaleXContentAnimation);
                        stNewScaleXContentAnimation.Begin();

                        DoubleAnimation newScaleYContentAnimation = new DoubleAnimation();
                        newScaleYContentAnimation.To = 1;
                        newScaleYContentAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(700));
                        newScaleYContentAnimation.EasingFunction = ee;
                        Storyboard stNewScaleYContentAnimation = new Storyboard();
                        Storyboard.SetTargetProperty(newScaleYContentAnimation, new PropertyPath(ScaleTransform.ScaleYProperty));
                        Storyboard.SetTarget(newScaleYContentAnimation, scale);
                        stNewScaleYContentAnimation.Children.Add(newScaleYContentAnimation);
                        stNewScaleYContentAnimation.Begin();

                        DoubleAnimation newOpacityContentAnimation = new DoubleAnimation();
                        newOpacityContentAnimation.To = 1;
                        newOpacityContentAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(700));
                        newOpacityContentAnimation.EasingFunction = ee;
                        Storyboard stNewOpacityContentAnimation = new Storyboard();
                        Storyboard.SetTargetProperty(newOpacityContentAnimation, new PropertyPath(ContentPresenter.OpacityProperty));
                        Storyboard.SetTarget(newOpacityContentAnimation, (UIElement)ContentTransition);
                        stNewOpacityContentAnimation.Children.Add(newOpacityContentAnimation);
                        stNewOpacityContentAnimation.Completed += lastAnimation_Completed;
                        stNewOpacityContentAnimation.Begin();

                    }
                    else
                    {
                        ScaleTransform scale = new ScaleTransform();
                        CtCenter.RenderTransform = scale;
                        CtCenter.RenderTransformOrigin = new Point(0.5, 0.5);
                        scale.ScaleX = 0.9;
                        scale.ScaleY = 0.9;

                        CtCenter.Content = ContentTransition;

                        DoubleAnimation newScaleXContentAnimation = new DoubleAnimation();
                        newScaleXContentAnimation.To = 1;
                        newScaleXContentAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(700));
                        newScaleXContentAnimation.EasingFunction = ee;
                        Storyboard stNewScaleXContentAnimation = new Storyboard();
                        Storyboard.SetTargetProperty(newScaleXContentAnimation, new PropertyPath(ScaleTransform.ScaleXProperty));
                        Storyboard.SetTarget(newScaleXContentAnimation, scale);
                        stNewScaleXContentAnimation.Children.Add(newScaleXContentAnimation);
                        stNewScaleXContentAnimation.Begin();

                        DoubleAnimation newScaleYContentAnimation = new DoubleAnimation();
                        newScaleYContentAnimation.To = 1;
                        newScaleYContentAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(700));
                        newScaleYContentAnimation.EasingFunction = ee;
                        Storyboard stNewScaleYContentAnimation = new Storyboard();
                        Storyboard.SetTargetProperty(newScaleYContentAnimation, new PropertyPath(ScaleTransform.ScaleYProperty));
                        Storyboard.SetTarget(newScaleYContentAnimation, scale);
                        stNewScaleYContentAnimation.Children.Add(newScaleYContentAnimation);
                        stNewScaleYContentAnimation.Completed += firstAnimation_Completed;
                        stNewScaleYContentAnimation.Begin();

                    }
                }
            }
        }

        void firstAnimation_Completed(object sender, EventArgs e)
        {
            isTransitioning = false;
        }

        void lastAnimation_Completed(object sender, EventArgs e)
        {
            CtCenter.ClearValue(ContentPresenter.RenderTransformProperty);
            newContentPresenter.Content = null;
            GrdMain.Children.Remove(newContentPresenter);
            CtCenter.Content = ContentTransition;
            CtCenter.Opacity = 1;
            isTransitioning = false;
        }
        #endregion

    }
}
